﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PID
{
    public partial class PIDSetter : UserControl
    {
        private double _kp;
        private double _ki;
        private double _kd;
        private int _iSampleCount;

        public PIDSetter()
        {
            InitializeComponent();
            
            pGainBox.Validating += new CancelEventHandler(GainBox_Validating);
            iGainBox.Validating += new CancelEventHandler(GainBox_Validating);
            dGainBox.Validating += new CancelEventHandler(GainBox_Validating);

            pGainBox.KeyDown += new KeyEventHandler(GainBox_KeyDown);
            iGainBox.KeyDown += new KeyEventHandler(GainBox_KeyDown);
            dGainBox.KeyDown += new KeyEventHandler(GainBox_KeyDown);

            this._kp = 1.00;
            this._ki = 0.00;
            this._kd = 0.00;
        }

        private void PIDSetter_Load(object sender, EventArgs e)
        {
        }

        private void GainBox_Validating(object sender, CancelEventArgs e)
        {
            TextBox sndr = (TextBox)sender;
            string value = (sndr).Text;
            double result = 0;
            bool success = double.TryParse(value, out result);
            
            if(!success)
                MessageBox.Show(String.Format("Invalid Entry! {0} is not a number.", value), "-_- | RTFM!");
               
            switch (sndr.Name)
            {
                case "pGainBox":
                    this._kp = success?result:this._kp;
                    sndr.Text = this._kp.ToString();
                    break;
                case "iGainBox":
                    this._ki = success?result:this._ki;
                    sndr.Text = this._ki.ToString();
                    break;
                case "dGainBox":
                    this._kd = success?result:this._kd;
                    sndr.Text = this._kd.ToString();
                    break;
            }
        }

        private void GainBox_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox sndr = (TextBox)sender;
            string value = (sndr).Text;
            double result = 0;
            bool success = double.TryParse(value, out result);

            if (e.KeyCode == Keys.Enter)
            {
                if (!success)
                    MessageBox.Show(String.Format("Invalid Entry! {0} is not a number.", value), "-_- | RTFM!");

                switch (sndr.Name)
                {
                    case "pGainBox":
                        this._kp = success ? result : this._kp;
                        sndr.Text = this._kp.ToString();
                        break;
                    case "iGainBox":
                        this._ki = success ? result : this._ki;
                        sndr.Text = this._ki.ToString();
                        break;
                    case "dGainBox":
                        this._kd = success ? result : this._kd;
                        sndr.Text = this._kd.ToString();
                        break;
                }
            }
            else if(e.KeyCode == Keys.Up)
            {
                double temp = Double.Parse(sndr.Text);
                temp += .01;
                sndr.Text = temp.ToString();
            }
            else if(e.KeyCode == Keys.Down)
            {
                double temp = Double.Parse(sndr.Text);
                temp -= .01;
                sndr.Text = temp.ToString();
            }
        }
    }
}
